﻿function AzureOnlineAccountDetailsView(context) {

    var dispatcher = $({});
    var originalAccount = ko.observable(null);
    var account = ko.observable(null);
    var opensCount = ko.observable(0);

    var name = ko.computed(function () {
        var acc = account();
        if (!acc) return '';
        return acc.storageName() || '';
    });

    var key = ko.computed(function () {
        var acc = account();
        if (!acc) return '';
        return acc.storageKey() || '';
    });

    var devStorage = ko.computed(function () {
        var acc = account();
        if (!acc) return false;
        return !!acc.devStorage();
    });

    var isNew = ko.computed(function () {
        return !originalAccount();
    });

    (function () {

        var prevValue = devStorage();
        var prevName = '';
        var prevKey = '';
        var originalOpensCount = null;

        opensCount.subscribe(function () {
            originalOpensCount = opensCount();
            prevName = name();
            prevKey = key();
            prevValue = devStorage();
        });

        devStorage.subscribe(function () {
            if (originalOpensCount != opensCount()) return;
            var val = devStorage();
            if (!prevValue && val) {
                prevName = name();
                prevKey = key();
                account().storageName('devstoreaccount1');
                account().storageKey('Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==');
            } else if (prevValue && !val) {
                account().storageName(prevName);
                account().storageKey(prevKey);
            }
            prevValue = val;
        });

    })();

    var triedToSubmit = ko.observable(false);

    var validName = ko.computed(function () {
        if (!triedToSubmit()) return true;
        if (showUniqueName()) return false;

        if (devStorage()) return true;

        if (showNameRequired()) return false;
        if (showMustContainAlphaNumericDashesOnly()) return false;
        if (showFirstLastDash()) return false;
        if (showConsecutiveDash()) return false;
        if (showLowerCase()) return false;
        if (showNameLength()) return false;
        return true;
    });

    var validKey = ko.computed(function () {
        if (!triedToSubmit()) return true;
        if (devStorage()) return true;

        if (showKeyRequired()) return false;
        if (showKeyBase64()) return false;
        return true;
    });

    var showNameRequired = ko.computed(function () {
        if (!triedToSubmit()) return false;
        var n = name().trim();
        if (utils.isNullOrWhiteSpace(n)) return true;
        return false;
    });

    var showKeyRequired = ko.computed(function () {
        if (!triedToSubmit()) return false;
        var n = key().trim();
        if (utils.isNullOrWhiteSpace(n)) return true;
        return false;
    });

    var showMustContainAlphaNumericDashesOnly = ko.computed(function () {
        if (!triedToSubmit()) return false;
        var n = name().trim();
        if (utils.isNullOrWhiteSpace(n)) return false;
        for (var i = 0; i < n.length; i++) {
            var ch = n.charAt(i);
            if (!utils.isAlpha(ch) && !utils.isDigit(ch) && ch != '-') {
                return true;
            }
        }
        return false;
    });

    var showFirstLastDash = ko.computed(function () {
        if (!triedToSubmit()) return false;
        var n = name().trim();
        if (utils.isNullOrWhiteSpace(n)) return false;
        var ch = n.charAt(0);
        if (ch == '-') {
            return true;
        }
        ch = n.charAt(n.length - 1);
        if (ch == '-') {
            return true;
        }
        return false;
    });

    var showConsecutiveDash = ko.computed(function () {
        if (!triedToSubmit()) return false;
        var n = name().trim();
        if (utils.isNullOrWhiteSpace(n)) return false;
        return n.indexOf('--') >= 0;
    });

    var showLowerCase = ko.computed(function () {
        if (!triedToSubmit()) return false;
        var n = name().trim();
        if (utils.isNullOrWhiteSpace(n)) return false;
        for (var i = 0; i < n.length; i++) {
            var ch = n.charAt(i);
            if (ch >= 'A' && ch <= 'Z') {
                return true;
            }
        }
        return false;
    });

    var showUniqueName = ko.computed(function () {
        if (!triedToSubmit()) return false;
        var n = name().trim();
        if (utils.isNullOrWhiteSpace(n)) return false;

        var acc = context.accounts.findByName(n);
        if (acc == null) return false;

        var original = originalAccount();
        var valid = original == acc;
        return !valid;
    });

    var showNameLength = ko.computed(function () {
        if (!triedToSubmit()) return false;
        var n = name().trim();
        if (utils.isNullOrWhiteSpace(n)) return false;
        if (n.length < 3) return true;
        if (n.length > 24) return true;
        return false;
    });

    var showKeyBase64 = ko.computed(function () {
        if (!triedToSubmit()) return false;
        var n = key().trim();
        if (utils.isNullOrWhiteSpace(n)) return false;
        var b64 = new Base64();
        return !b64.validate(n);
    });

    function init() {
        triedToSubmit(false);
        opensCount(opensCount() + 1);
    }

    function validate() {
        triedToSubmit(true);
        return validName() && validKey();
    }

    function submit() {
        var acc = account();
        if (validate(acc)) {
            var original = originalAccount();
            if (original) {
                acc.cloneTo(original);
            } else {
                originalAccount(acc);
                context.accounts.accounts.push(acc);
            }
            context.accounts.account(acc);
            dispatcher.trigger('success');
        }
    }

    this.account = account;
    this.submit = submit;
    this.isNew = isNew;

    this.forAccount = function (acc) {
        originalAccount(acc);
        account(acc.clone());
        init();
    };

    this.forNew = function () {
        originalAccount(null);
        account(new AzureOnlineAccount());
        init();
    };

    this.addOnSuccess = function (handler) {
        dispatcher.bind('success', handler);
    };

    this.validName = validName;
    this.validKey = validKey;
    this.hasErrors = ko.computed(function () {
        return !validName() || !validKey();
    });
    this.showNameRequired = showNameRequired;
    this.showKeyRequired = showKeyRequired;
    this.showMustContainAlphaNumericDashesOnly = showMustContainAlphaNumericDashesOnly;
    this.showFirstLastDash = showFirstLastDash;
    this.showConsecutiveDash = showConsecutiveDash;
    this.showLowerCase = showLowerCase;
    this.showNameLength = showNameLength;
    this.showUniqueName = showUniqueName;
    this.showKeyBase64 = showKeyBase64;
}

function AzureOnlineAccount() {
    var self = this;

    var storageName = ko.observable('');

    var storageKey = ko.observable('');

    var devStorage = ko.observable(false);

    function cloneTo(other) {
        other.storageName(storageName());
        other.storageKey(storageKey());
        other.devStorage(devStorage());
        return other;
    }

    function clone() {
        return cloneTo(new AzureOnlineAccount());
    }

    self.storageName = storageName;
    self.storageKey = storageKey;
    self.devStorage = devStorage;
    self.clone = clone;
    self.cloneTo = cloneTo;
}